<?php
namespace lib;
use App\Model\MailModel;
/**
 * 数据模型类的父类，所有的数据模型类要继承此类
 *
 * @package lib
 */
class Model
{
    private $pdo;
    protected $table;
    protected $pk;

    /**
     * model constructor.
     * @param $table 表名
     * @param $pk 主键名
     */
    public function __construct()
    {
//        require 'functions.php';
        $config=require '../config/database.php';
        $host=$config['host'];
        $dbname=$config['dbname'];
        $dsn="mysql:host=$host;dbname=$dbname";
        $this->pdo = new \Pdo($dsn,$config['username'],$config['password']);
    }

    public function get(Array $data=[],$condition='1'){
        //select * from students where id=4 and name='小黄';
        $sets='';
        foreach($data as $k=>$v){
            $sets.=$k."='".$v."' and ";
        }
        $sets=rtrim($sets,'and ');
        if(empty($data)){
            $sql="select * from ".$this->table;
        }else{

            if ($condition==1) {
                $sql="select * from ".$this->table." where ".$sets.";";
            }
            else{
                $sql="select * from ".$this->table." where ".$sets." order by ".$condition.";";
            }
              
        }

        $res=$this->pdo->query($sql);
        $rows=$res->fetchAll(\PDO::FETCH_ASSOC);
        return  $rows;
    }


    /**
     * @param array $data 要操作的数据
     */
    public function save(Array $data){
        $sets='';

        foreach($data as $k=>$v){
            $sets.=$k."='".$v."',";

        }
        $sets=rtrim($sets,',');

        if(isset($data[$this->pk])){  //如果用户有传主键字段就为update
            $sql="update ".$this->table." set ".$sets." where ".$this->pk."=".$data[$this->pk].";";
        }
        else{
            $sql="insert into ".$this->table." set ".$sets.";";
        }      
        $res=$this->pdo->exec($sql);
   
        if($res){
            return '修改成功!';
        }else{

            echo '失败了！';
            exit;
            return '修改失败!';
        }
    }

 public function join(Array $data,Array $condition=[]){
        $joins='';
        $conditions='';
        foreach($data as $k=>$v){
            $joins.=' join '.$k.' on '.$this->table.'.'.$this->pk.'='.$k.'.'.$v;
        }
        if(!empty($condition)) {
            $where = ' where ';
            foreach ($condition as $k => $v) {
                $conditions .= $k . "='" . $v . "' and ";
            }
            $conditions = rtrim($conditions, 'and ');
            $conditions = $where . $conditions;
        }

        $sql="select * from ".$this->table.$joins.$conditions.";";
        $res=$this->pdo->query($sql);
        $rows=$res->fetchAll(\PDO::FETCH_ASSOC);
        return  $rows;
    }

    public function deletes(Array $data){
        $sets='';
        foreach($data as $k=>$v){
            $sets.=$k."='".$v."' and ";
        }
        $sets=rtrim($sets,'and ');
        $sql="delete from ".$this->table." where ".$sets.";";

        $res=$this->pdo->exec($sql);
        if($res){
            return '删除成功!';
        }else{

            return  '删除失败!';
        }
    }

       public  function pangination($user_id,$ls)
    {

        // 每页显示的数据数目
        $pageSize =8;
        
        // 总页数
         $pageCount=ceil(count($ls)/$pageSize);
        // 当前页码   
        $pn = isset($_GET['pn']) ? $_GET['pn'] : 1;

        // 分页查询数据
        $offset = ($pn -1) * $pageSize;

        //使用数组截取函数获得每页的邮件
        $messages=array_slice($ls,$offset,$pageSize);

        $view = [
            'pn' => $pn,
            'pageCount' => $pageCount,
            'messages' => $messages
        ];
        return $view;
    }
    public function count_email($emails){
        $count = [];
        $count['number'] =0;
        
        if(!empty($emails)){
        foreach ($emails as $key => $value) {
          if ($value['status']==0) {
            $count['userInfo'][$key]['id']=$value['id'] ;
            $count['userInfo'][$key]['name']=$value['send_email'] ;
            $count['userInfo'][$key]['create_at']=$value['create_at'] ;
            $count['number']++;
          }
        }
        if (isset($count['userInfo'])) {
           $count['userInfo'] = array_slice($count['userInfo'],0,5);
        }
     }
     else{
            $count['userInfo'] = [];
        }
         return $count;

        }

}
